<?php

namespace front\modules\app\controllers;

use Yii;
use front\models\UserModel;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use users\services\UserService;
use yii\helpers\ArrayHelper;
/**
 * UserController implements the CRUD actions for UserModel model.
 */
class UserController extends WebBaseController
{

    public $enableCsrfValidation = false;
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }


    /**
     * 用户登录
     * @param mobile
     * @param password
     * @return bool
     */
    public function actionIndex()
    {
        echo 111;

    }



    /**
     * 获取用户信息
     */
    function actionGetinfo(){
        $data = yii::$app->request->get();
        $data['user_id'] = $this->user_id;
        $info = UserService::getUserInfo($data['user_id']);
        $info['result'] = ArrayHelper::toArray($info['result']);
        if($info['code']==0){
            WebBaseController::getAjaxData(1,$info['message'],$info['result']);
        }
    }


    /**
     *  user update info
     *
     * @return array
     */
    public function actionUpdate()
    {
        $param = Yii::$app->request->post();
        $info =  UserService::updateUserInfo($param);
        if($info['code']==1){
            WebBaseController::getAjaxData(2,$info['message'],$info['result']);
        }
        if($info['code']==0){
            WebBaseController::getAjaxData(1,$info['message'],$info['result']);
        }
    }



    /**
     * Displays a single UserModel model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new UserModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new UserModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing UserModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdateinfo($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing UserModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the UserModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return UserModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = UserModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
